package zld369504.tw.m_team.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

import zld369504.tw.m_team.R;
import zld369504.tw.m_team.api.MTeamHttpsApiImpl;
import zld369504.tw.m_team.dao.TorrentDetail;

public class TorrentIMDbFragment extends BaseFragment {

    private TorrentDetail detail;

    private View rootView;
    private WebView webView;

    public static TorrentIMDbFragment getInstance(TorrentDetail detail){
        TorrentIMDbFragment fragment=new TorrentIMDbFragment();
        Bundle bundle=new Bundle();
        bundle.putSerializable("detail",detail);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle=getArguments();
        if(bundle!=null){
            detail= (TorrentDetail) bundle.getSerializable("detail");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_torrent_imdb, container, false);
        webView = rootView.findViewById(R.id.webView);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (detail != null && !TextUtils.isEmpty(detail.getDescription())) {
            WebSettings ws = webView.getSettings();
            ws.setJavaScriptEnabled(false);
            ws.setAllowFileAccess(true);
            ws.setSupportZoom(true);
            ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
            ws.setDefaultTextEncodingName("utf-8"); //设置文本编码
            ws.setAppCacheEnabled(true);
            //这种写法可以正确解码
            webView.loadDataWithBaseURL(MTeamHttpsApiImpl.baseUrl, detail.getImdbInfo(), "text/html; charset=UTF-8", null, null);
        }
    }
}
